<?php

/**
 * patriarch actions.
 *
 * @package    jiazhang
 * @subpackage patriarch
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class patriarchActions extends sfActions
{
    
    public function executeMy_patriarch(sfWebRequest $request) {
        $articles = array();
        if($request->isMethod('POST')){
            $page_size = (int)$request->getParameter('page_size',11);
            $page = (int)$request->getParameter('page',1);
            $user_id = (int)$request->getParameter('person_id',0);
            $dql = Doctrine::getTable('PatriarchArticle')->createQuery()->where('user_id = ?',$user_id)->andWhere('display = 1')->orderBy('created_at desc');
            $this->pager = new sfDoctrinePager('PatriarchArticle',$page_size);
            $this->pager->setQuery($dql);
            $this->pager->setPage($page);
            $this->pager->init();
            $i=0;
      
      foreach($this->pager->getResults() as $article){
            $user_id = $article->getUserId();
            $user = Doctrine::getTable('User')->findOneById($user_id);
            if(!$user){
                continue;
            }
            
            $articles[$i]['id'] = $article->getId();
            $articles[$i]['content'] = $article->getContent();
            $articles[$i]['createTime'] = strtotime($article->getCreatedAt());
            $articles[$i]['commentNum'] = $article->getCommentNum();
            $articles[$i]['picturs'] = array();
            $pics = Doctrine::getTable('PatriarchPic')->findByPId($article->getId());
            
            if(count($pics) > 0){
                foreach($pics as $pic){
                    $articles[$i]['picturs'][] = $pic->getImageUrl();
                }
            }
            
            
            $articles[$i]['person'] = array();
            $articles[$i]['person']['id'] = $user->getId();
            $articles[$i]['person']['name'] = $user->getNickname();
            $articles[$i]['person']['icon'] = $user->getCover();
            $articles[$i]['person']['city'] = $user->getCity();
            $articles[$i]['person']['children'] = array();
            $childrens = $user->getChilds();
            
            if(count($childrens) > 0){
                $j=0;
                foreach($childrens as $children){
                    $articles[$i]['person']['children'][$j]['name'] = $children->getName();
                    $articles[$i]['person']['children'][$j]['age'] = $children->getChildOld();
                    $articles[$i]['person']['children'][$j]['sex'] = $children->getSexToString();
                    $j++;
                }
            }
           
           $i++;
        }
        }
        return $this->renderText(json_encode($articles));
    }
    
    public function executeShow(sfWebRequest $request){
        $id = (int)$request->getParameter('patriarch_id',0);
        $patriarch = Doctrine::getTable('PatriarchArticle')->findOneById($id);
        $articles = array();
        
        if($patriarch){
            $article = $patriarch;
            $user_id = $article->getUserId();
            $user = Doctrine::getTable('User')->findOneById($user_id);
            
            if(!$user){
               return $this->renderText(json_encode($articles));
            }
            
            $articles['id'] = $article->getId();
            $articles['content'] = $article->getContent();
            $articles['createTime'] = strtotime($article->getCreatedAt());
            $articles['commentNum'] = $article->getCommentNum();
            $articles['picturs'] = array();
            $pics = Doctrine::getTable('PatriarchPic')->findByPId($article->getId());
            
            if(count($pics) > 0){
                foreach($pics as $pic){
                    $articles['picturs'][] = $pic->getImageUrl();
                }
            }
            
            
            $articles['person'] = array();
            $articles['person']['id'] = $user->getId();
            $articles['person']['name'] = $user->getNickname();
            $articles['person']['icon'] = $user->getCover();
            $articles['person']['city'] = $user->getCity();
            $articles['person']['children'] = array();
            $childrens = $user->getChilds();
            
            if(count($childrens) > 0){
                $j=0;
                foreach($childrens as $children){
                    $articles['person']['children'][$j]['name'] = $children->getName();
                    $articles['person']['children'][$j]['age'] = $children->getChildOld();
                    $articles['person']['children'][$j]['sex'] = $children->getSexToString();
                    $j++;
                }
            }
        }
        
        return $this->renderText(json_encode($articles));
    }
    
    public function executeDelete(sfWebRequest $request){
        $msg = array('error'=>0,'msg'=>'');
        if($request->isMethod('POST')){
            $user_id = (int)$request->getParameter('person_id',0);
            $patriarch_id = (int)$request->getParameter('patriarch_id',0);
            $slat_key = $request->getParameter('salt',0);
            $key = md5($user_id."+api_etongxue_delete+".$patriarch_id);
            
            $patriarch = Doctrine::getTable('PatriarchArticle')->findOneById($patriarch_id);
            //@tudo:可以根据用户ID和家长圈ID一起查找，然后统计返回数据来优化IF，减少一层IF判断
            if(!$patriarch){
                $msg['error'] = 1;
                $msg['msg'] = '家长圈未找到！';
            }else{
                
                if($patriarch->getUserId() != $user_id ){
                    $msg['error'] = 1;
                    $msg['msg'] = '您不是作者，无法删除别人的文章！';
                }else{
                    if($slat_key == $key){
                        Doctrine::getTable('PatriarchPic')->createQuery()->where("p_id = ?",$patriarch->getId())->delete()->execute();
                        Doctrine::getTable('PatriarchComment')->createQuery()->where("p_id = ?",$patriarch->getId())->delete()->execute();
                        $patriarch->delete();
                        $msg['error'] = 0;
                        $msg['msg'] = '文章删除成功！';
                    }else{
                        $msg['error'] = 1;
                        $msg['msg'] = '密钥验证错误，删除失败！';
                    }
                }
            }
        }
        return $this->renderText(json_encode($msg));
    }
    
    
  //家长评论接口
  public function executeCreate_comment(sfWebRequest $request){
      if($request->isMethod('POST')){
          $content = trim($request->getParameter('content',''));
          $user_id = (int)$request->getParameter('user_id',0);
          $p_id = (int)$request->getParameter('p_id',0);
          $patriarch_commend = new PatriarchComment();
          $patriarch_commend->setUserId($user_id)->setPId($p_id)->setContent($content)->save();
          $msg = array('error'=>0,'msg'=>'评论发布成功！');
          return $this->renderText(json_encode($msg));
      }
  }  
    
  public function executeList(sfWebRequest $request){
      $page_size = (int)$request->getParameter('page_size',11);
      $dql = Doctrine::getTable('PatriarchArticle')->createQuery()->where('display = 1')->orderBy('created_at desc');
      $page = (int)$request->getParameter('page',1);
      $comment_num = (int)$request->getParameter('comment_num',0);
      $this->pager = new sfDoctrinePager('PatriarchArticle',$page_size);
      $this->pager->setQuery($dql);
      $this->pager->setPage($page);
      $this->pager->init();
      $articles = array();
      $i=0;
      
      foreach($this->pager->getResults() as $article){
            $user_id = $article->getUserId();
            $user = Doctrine::getTable('User')->findOneById($user_id);
            if(!$user){
                continue;
            }
            
            $articles[$i]['id'] = $article->getId();
            $articles[$i]['content'] = $article->getContent();
            $articles[$i]['createTime'] = strtotime($article->getCreatedAt());
            $articles[$i]['commentNum'] = $article->getCommentNum();
            $articles[$i]['picturs'] = array();
            $pics = Doctrine::getTable('PatriarchPic')->findByPId($article->getId());
            
            if(count($pics) > 0){
                foreach($pics as $pic){
                    $articles[$i]['picturs'][] = $pic->getImageUrl();
                }
            }
            
            
            $articles[$i]['person'] = array();
            $articles[$i]['person']['id'] = $user->getId();
            $articles[$i]['person']['name'] = $user->getNickname();
            $articles[$i]['person']['icon'] = $user->getCover();
            $articles[$i]['person']['city'] = $user->getCity();
            $articles[$i]['person']['children'] = array();
            $childrens = $user->getChilds();
            
            if(count($childrens) > 0){
                $j=0;
                foreach($childrens as $children){
                    $articles[$i]['person']['children'][$j]['name'] = $children->getName();
                    $articles[$i]['person']['children'][$j]['age'] = $children->getChildOld();
                    $articles[$i]['person']['children'][$j]['sex'] = $children->getSexToString();
                    $j++;
                }
            }
            
           $articles[$i]['commentList'] = array();
           $comments = $article->getComments($comment_num);
           
           if(count($comments) > 0){
                $k = 0;
                foreach($comments as $comment){
                    if(!$comment->getUser())  continue;
                    $articles[$i]['commentList'][$k]['content'] = $comment->getContent();
                    $articles[$i]['commentList'][$k]['person']['id'] = $comment->getUser()->getId();
                    $articles[$i]['commentList'][$k]['person']['name'] = $comment->getUser()->getNickname();
                    $k++;
                }
           }
           
           $i++;
        }
      return $this->renderText(json_encode($articles));
  }
  
 /**
  * 家长圈发布文章
  *
  * @param sfRequest $request A request object
  */
  public function executeCreate(sfWebRequest $request)
  {
    if($request->isMethod('POST')){
        $content = $request->getParameter('content','');
        $user_id = (int)$request->getParameter('user_id',0);
        $picturs = json_decode(trim($request->getParameter('picturs','[]')),true);
        $msg = array('error'=>'','msg'=>'');
        
        if($user_id == 0){
              $msg['error'] = 1;
              $msg['msg'] = '用户不存在，请联系管理员';
              return $this->renderText(json_encode($msg));
        }
       
        $patriarch = new PatriarchArticle();
        $patriarch->setUserId($user_id)->setContent($content)->setCommentNum(0)->setDisplay(1)->save();
   
        if(count($picturs) > 0){
            foreach($picturs as $pic){
                $pic_obj = new PatriarchPic();
                $pic_obj->setPId($patriarch->getId())->setImageUrl($pic)->save();
            }
        }
        
        $msg['error'] = 0;
        $msg['msg'] = '数据保存成功！';
        return $this->renderText(json_encode($msg));
    }
  }
}
